package com.sfzy.controller;

import com.github.pagehelper.PageInfo;
import com.sfzy.entity.Question;
import com.sfzy.service.QuestionService;
import com.sfzy.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("question")
public class QuestionController {
    @Autowired
    private QuestionService questionService;
//    增
    @PostMapping
    public R addQuestion(@RequestBody Question question) {
        int i = questionService.addQuestion(question);
        if(i == 0){
            return R.fail("增加失败！");
        }
        return R.ok("增加成功！");
    }
//    删
    @DeleteMapping("{id}")
    public R deleteQuestion(@PathVariable("id") int id) {
        int i = questionService.deleteQuestion(id);
        if(i == 0){
            return R.fail("删除失败！");
        }
        return R.ok("删除成功！");
    }
//    改
    @PutMapping
    public R updateQuestion(@RequestBody Question question) {
        int i = questionService.updateQuestion(question);
        if(i == 0){
            return R.fail("修改失败！");
        }
        return R.ok("修改成功！");
    }
//    根据id查询
    @GetMapping("{id}")
    public R getQuestionById(@PathVariable("id") int id) {
        Question question = questionService.getQuestionById(id);
        return R.ok(question);
    }
//    分页查询
    @GetMapping("page")
    public R getAllQuestion(Question question,
                            @RequestParam(required = false,defaultValue = "1") int pageNum,
                            @RequestParam(required = false,defaultValue = "5") int pageSize) {
        PageInfo<Question> questions = questionService.getQuestions(question, pageNum, pageSize);
        return R.ok(questions);
    }
}
